package com.zscat.mallplus.service.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.entity.vo.oms.CartItmes;
import com.zscat.mallplus.entity.vo.pms.ItmsVo;
import com.zscat.mallplus.entity.vo.pms.PmsSkuStockVo;
import com.zscat.mallplus.entity.vo.pms.ProductAttributeValueVo;
import com.zscat.mallplus.exception.ApiMallPlusException;
import com.zscat.mallplus.oms.entity.OmsCartItem;
import com.zscat.mallplus.oms.mapper.OmsCartItemMapper;
import com.zscat.mallplus.oms.vo.CartProduct;
import com.zscat.mallplus.oms.vo.CartPromotionItem;
import com.zscat.mallplus.pms.entity.*;
import com.zscat.mallplus.pms.mapper.PmsFavoriteMapper;
import com.zscat.mallplus.pms.mapper.PmsProductAttributeValueMapper;
import com.zscat.mallplus.pms.mapper.PmsProductMapper;
import com.zscat.mallplus.pms.mapper.PmsSkuStockMapper;
import com.zscat.mallplus.pms.vo.PromotionProduct;
import com.zscat.mallplus.service.oms.service.IOmsCartItemService;
import com.zscat.mallplus.service.pms.service.IPmsSkuStockService;
import com.zscat.mallplus.service.ums.service.IUmsMemberService;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import com.zscat.mallplus.vo.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author zscat
 * @since 2019-04-17
 */
@Service
public class OmsCartItemServiceImpl extends ServiceImpl<OmsCartItemMapper, OmsCartItem> implements IOmsCartItemService {
    @Resource
    private OmsCartItemMapper cartItemMapper;
    @Resource
    private IUmsMemberService memberService;
    @Resource
    private PmsProductMapper pmsProductMapper;
    @Autowired
    private IPmsSkuStockService pmsSkuStockService;

    @Resource
    private PmsFavoriteMapper pmsFavoriteMapper;

    @Resource
    private PmsSkuStockMapper pmsSkuStockMapper;

    @Resource
    private PmsProductAttributeValueMapper pmsProductAttributeValueMapper;


    @Override
    public OmsCartItem add(OmsCartItem cartItem) {
        UmsMember currentMember = memberService.getNewCurrentMember();
        cartItem.setMemberId(currentMember.getId());
        cartItem.setMemberNickname(currentMember.getNickname());
        cartItem.setDeleteStatus(0);
        PmsProduct pmsProduct = pmsProductMapper.selectById(cartItem.getProductId());
        if (org.apache.commons.lang.StringUtils.isBlank(cartItem.getProductPic())) {
            cartItem.setProductPic(pmsProduct.getPic());
        }
        cartItem.setProductBrand(pmsProduct.getBrandName());
        cartItem.setProductName(pmsProduct.getName());
        cartItem.setProductSn(pmsProduct.getProductSn());
        cartItem.setProductSubTitle(pmsProduct.getSubTitle());
        cartItem.setProductCategoryId(pmsProduct.getProductCategoryId());
        OmsCartItem existCartItem = getCartItem(cartItem);
        if (existCartItem == null) {
            cartItem.setCreateDate(new Date());
            cartItemMapper.insert(cartItem);
        } else {
            cartItem.setModifyDate(new Date());
            existCartItem.setQuantity(existCartItem.getQuantity() + cartItem.getQuantity());
            cartItemMapper.updateById(existCartItem);
            return existCartItem;
        }
        return cartItem;
    }

    /**
     * 根据会员id,商品id和规格获取购物车中商品
     */
    private OmsCartItem getCartItem(OmsCartItem cartItem) {
        OmsCartItem example = new OmsCartItem();
        example.setProductId(cartItem.getProductId());
        example.setDeleteStatus(0);

        if (!StringUtils.isEmpty(cartItem.getSp1())) {
            example.setSp1(cartItem.getSp1());
        }
        if (!StringUtils.isEmpty(cartItem.getSp2())) {
            example.setSp2(cartItem.getSp2());
        }
        if (!StringUtils.isEmpty(cartItem.getSp3())) {
            example.setSp3(cartItem.getSp3());
        }
        List<OmsCartItem> cartItemList = cartItemMapper.selectList(new QueryWrapper<>(example));
        if (!CollectionUtils.isEmpty(cartItemList)) {
            return cartItemList.get(0);
        }
        return null;
    }

    @Override
    public List<OmsCartItem> list(Long memberId, List<Long> ids) {


        OmsCartItem example = new OmsCartItem();
        example.setMemberId(memberId);
        example.setDeleteStatus(0);
        if (ids != null && ids.size() > 0) {
            return cartItemMapper.selectList(new QueryWrapper<>(example).in("id", ids));
        }
        return cartItemMapper.selectList(new QueryWrapper<>(example));
    }


    private PageInfo<CartItmes> packingCar(List<OmsCartItem> omsCartItems, Integer pageIndex, Integer pageSize) {

        List<CartItmes> result = new ArrayList<>();
        if (omsCartItems.size() > 0) {

            for (OmsCartItem omsCartItem : omsCartItems) {

                CartItmes cartItmes = new CartItmes();

                //商品id
                Long productId = omsCartItem.getProductId();

                //库存id
                Long productSkuId = omsCartItem.getProductSkuId();

                //判断商品是否已经下架
                PmsProduct pmsProduct = pmsProductMapper.selectById(productId);


                if (pmsProduct.getPublishStatus() == 0) {
                    cartItmes.setGoodsType(1);
                } else {
                    //判断库存是否满足
                    PmsSkuStock pmsSkuStock = pmsSkuStockMapper.selectById(productSkuId);
                    Integer stock = pmsSkuStock.getStock();
                    if (stock == 0) {
                        cartItmes.setGoodsType(2);
                        cartItmes.setSkuMun(stock);
                    } else {
                        cartItmes.setSkuMun(stock);
                        cartItmes.setGoodsType(0);
                    }
                }

                BeanUtils.copyProperties(omsCartItem, cartItmes);


                List<PmsSkuStockVo> pmsSkuStockVoList = new ArrayList<>();
                List<ProductAttributeValueVo> productAttributeValueVoList = new ArrayList<>();


                //拿到商品的属性
                List<PmsProductAttributeValue> attributeValues = findGoodsAttribute("product_id", productId);

                //拿到商品库存中所有的规格
                List<PmsSkuStock> pmsSkuStockList = findSkustockInfo("product_id", productId);

                if (pmsSkuStockList.size() > 0) {

                    for (PmsSkuStock pmsSkuStock : pmsSkuStockList) {
                        List<String> spList = new ArrayList<>();

                        PmsSkuStockVo pmsSkuStockVo = new PmsSkuStockVo();

                        String sp1 = pmsSkuStock.getSp1();
                        String sp2 = pmsSkuStock.getSp2();
                        String sp3 = pmsSkuStock.getSp3();
                        String sp4 = pmsSkuStock.getSp4();

                        if (!StringUtils.isEmpty(sp1)) {
                            spList.add(sp1);
                        }
                        if (!StringUtils.isEmpty(sp2)) {
                            spList.add(sp2);
                        }
                        if (!StringUtils.isEmpty(sp3)) {
                            spList.add(sp3);
                        }
                        if (!StringUtils.isEmpty(sp4)) {
                            spList.add(sp4);
                        }

                        BeanUtils.copyProperties(pmsSkuStock, pmsSkuStockVo);

                        pmsSkuStockVo.setDifference(spList);

                        pmsSkuStockVoList.add(pmsSkuStockVo);
                    }
                    cartItmes.setPmsSkuStockVoList(pmsSkuStockVoList);
                    //todo
                }


                if (attributeValues.size() > 0) {

                    for (PmsProductAttributeValue attributeValue : attributeValues) {
                        List<ItmsVo> listValues = new ArrayList<>();

                        ProductAttributeValueVo productAttributeValueVo = new ProductAttributeValueVo();

                        String value = attributeValue.getValue();

                        if (!StringUtils.isEmpty(value)) {
                            String[] split = value.split(",");
                            for (String s : split) {
                                ItmsVo itmsVo = new ItmsVo();
                                itmsVo.setName(s);
                                listValues.add(itmsVo);
                            }

                        }

                        BeanUtils.copyProperties(attributeValue, productAttributeValueVo);
                        productAttributeValueVo.setItem(listValues);

                        productAttributeValueVoList.add(productAttributeValueVo);

                    }

                    cartItmes.setProductAttributeValueVoList(productAttributeValueVoList);

                }
                result.add(cartItmes);
            }
        }
        return new PageInfo<>(pageIndex, pageSize, result);
    }

    private List<PmsProductAttributeValue> findGoodsAttribute(String column, Object value) {
        QueryWrapper<PmsProductAttributeValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, value);
        return pmsProductAttributeValueMapper.selectList(queryWrapper);
    }

    private List<PmsSkuStock> findSkustockInfo(String column, Object value) {
        QueryWrapper<PmsSkuStock> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(column, value);

        return pmsSkuStockMapper.selectList(queryWrapper);
    }

    @Override
    public OmsCartItem selectById(Long id) {
        return cartItemMapper.selectById(id);
    }

    @Override
    public List<OmsCartItem> listPromotion(Long memberId, List<Long> ids) {
        List<OmsCartItem> cartItemList = list(memberId, ids);
        List<CartPromotionItem> cartPromotionItemList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(cartItemList)) {
            //   cartPromotionItemList = this.calcCartPromotion(cartItemList);
        }
        return cartItemList;
    }

    @Override
    public int updateQuantity(Long id, Long memberId, Integer quantity) {
        OmsCartItem cartItem = new OmsCartItem();
        cartItem.setQuantity(quantity);
        cartItem.setId(id);

        return cartItemMapper.updateById(cartItem);
    }

    @Override
    public CommonResult updateQuantity1(Long id, Long memberId, Integer quantity) {
        CommonResult commonResult = new CommonResult();

        OmsCartItem omsCartItem = cartItemMapper.selectById(id);


        Long productSkuId = omsCartItem.getProductSkuId();

        PmsSkuStock pmsSkuStock = pmsSkuStockMapper.selectById(productSkuId);

        Integer stock = pmsSkuStock.getStock();

        if (quantity > stock) {
            return commonResult.failedIsNull("已经超出库存上线");
        }

        OmsCartItem cartItem = new OmsCartItem();
        cartItem.setQuantity(quantity);
        cartItem.setId(id);

        cartItemMapper.updateById(cartItem);

        return commonResult.success(stock);


    }


    @Override
    public int delete(Long memberId, List<Long> ids) {
        return cartItemMapper.deleteBatchIds(ids);
    }

    @Override
    public CartProduct getCartProduct(Long productId) {
        return pmsProductMapper.getCartProduct(productId);
    }

    @Override
    public int updateAttr(OmsCartItem cartItem) {
        //删除原购物车信息
        OmsCartItem updateCart = new OmsCartItem();
        updateCart.setId(cartItem.getId());
        updateCart.setModifyDate(new Date());
        updateCart.setDeleteStatus(1);
        cartItemMapper.updateById(updateCart);
        cartItem.setId(null);
        add(cartItem);
        return 1;
    }

    @Override
    public CommonResult updateAttr1(Long memberId, Long id, Long skuId) {
        CartItmes cartItmes = new CartItmes();
        CommonResult commonResult = new CommonResult();
        List<ProductAttributeValueVo> productAttributeValueVoList = new ArrayList<>();
        List<PmsSkuStockVo> pmsSkuStockVoList = new ArrayList<>();

        PmsSkuStock pmsSkuStock = pmsSkuStockMapper.selectById(skuId);

        if (pmsSkuStock.getStock() == 0) {
            return commonResult.failed("该商品已无库存");
        }

        OmsCartItem memberCart = findMemberCart("member_id", memberId, "id", id);

        if (memberCart == null) {
            return commonResult.failed("无当前购物车信息");
        }

        PmsProduct pmsProduct = pmsProductMapper.selectById(memberCart.getProductId());


        memberCart.setChecked(1);
        memberCart.setProductSkuId(skuId);
        memberCart.setPrice(pmsSkuStock.getPrice());
        memberCart.setProductSkuCode(pmsSkuStock.getSkuCode());
        memberCart.setProductAttr(pmsSkuStock.getMeno());
        memberCart.setProductPic(pmsSkuStock.getPic());
        memberCart.setPresentPrice(pmsSkuStock.getPrice());
        memberCart.setSp1(pmsSkuStock.getSp1());
        memberCart.setSp2(pmsSkuStock.getSp2());
        memberCart.setSp3(pmsSkuStock.getSp3());
        memberCart.setProductName(pmsSkuStock.getProductName());
        memberCart.setProductCategoryId(pmsProduct.getProductCategoryId());
        memberCart.setProductBrand(pmsProduct.getBrandName());
        memberCart.setStoreId(pmsProduct.getStoreId());
        memberCart.setStoreName(pmsProduct.getStoreName());

        int i = cartItemMapper.updateById(memberCart);

        if (i == 1) {

            BeanUtils.copyProperties(memberCart, cartItmes);

            //拿到商品的属性
            List<PmsProductAttributeValue> attributeValues = findGoodsAttribute("product_id", memberCart.getProductId());

            //拿到商品库存中所有的规格
            List<PmsSkuStock> pmsSkuStockList = findSkustockInfo("product_id", memberCart.getProductId());

            if (pmsSkuStockList.size() > 0) {

                for (PmsSkuStock pmsSkuStock1 : pmsSkuStockList) {
                    List<String> spList = new ArrayList<>();

                    PmsSkuStockVo pmsSkuStockVo = new PmsSkuStockVo();

                    String sp1 = pmsSkuStock1.getSp1();
                    String sp2 = pmsSkuStock1.getSp2();
                    String sp3 = pmsSkuStock1.getSp3();
                    String sp4 = pmsSkuStock1.getSp4();

                    if (!StringUtils.isEmpty(sp1)) {
                        spList.add(sp1);
                    }
                    if (!StringUtils.isEmpty(sp2)) {
                        spList.add(sp2);
                    }
                    if (!StringUtils.isEmpty(sp3)) {
                        spList.add(sp3);
                    }
                    if (!StringUtils.isEmpty(sp4)) {
                        spList.add(sp4);
                    }

                    BeanUtils.copyProperties(pmsSkuStock, pmsSkuStockVo);

                    pmsSkuStockVo.setDifference(spList);

                    pmsSkuStockVoList.add(pmsSkuStockVo);
                }
                cartItmes.setPmsSkuStockVoList(pmsSkuStockVoList);
                //todo
            }


            if (attributeValues.size() > 0) {

                for (PmsProductAttributeValue attributeValue : attributeValues) {
                    List<ItmsVo> listValues = new ArrayList<>();

                    ProductAttributeValueVo productAttributeValueVo = new ProductAttributeValueVo();

                    String value = attributeValue.getValue();

                    if (!StringUtils.isEmpty(value)) {
                        String[] split = value.split(",");
                        for (String s : split) {
                            ItmsVo itmsVo = new ItmsVo();
                            itmsVo.setName(s);
                            listValues.add(itmsVo);
                        }

                    }

                    BeanUtils.copyProperties(attributeValue, productAttributeValueVo);
                    productAttributeValueVo.setItem(listValues);

                    productAttributeValueVoList.add(productAttributeValueVo);

                }
                cartItmes.setProductAttributeValueVoList(productAttributeValueVoList);
            }
            return commonResult.success(cartItmes);
        } else {
            return commonResult.failed("失败");
        }
    }

    private OmsCartItem findMemberCart(String column, Object value, String column1, Object value1) {
        QueryWrapper<OmsCartItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, value).eq(column1, value1);
        return cartItemMapper.selectOne(queryWrapper);
    }


    @Override
    public int clear(Long memberId) {
        return cartItemMapper.delete(new QueryWrapper<OmsCartItem>().eq("member_id", memberId));
    }

    @Override
    public OmsCartItem addCart(OmsCartItem cartItem) {
        UmsMember currentMember = memberService.getNewCurrentMember();
        cartItem.setMemberId(currentMember.getId());
        cartItem.setMemberNickname(currentMember.getNickname());
        cartItem.setDeleteStatus(0);
        PmsProduct pmsProduct = pmsProductMapper.selectById(cartItem.getProductId());
        if (org.apache.commons.lang.StringUtils.isBlank(cartItem.getProductPic())) {
            cartItem.setProductPic(pmsProduct.getPic());
        }
        cartItem.setProductBrand(pmsProduct.getBrandName());
        cartItem.setProductName(pmsProduct.getName());
        cartItem.setProductSn(pmsProduct.getProductSn());
        cartItem.setProductSubTitle(pmsProduct.getSubTitle());
        cartItem.setProductCategoryId(pmsProduct.getProductCategoryId());
        OmsCartItem existCartItem = getCartItem(cartItem);
        if (existCartItem == null) {
            cartItem.setCreateDate(new Date());
            cartItemMapper.insert(cartItem);
        } else {
            cartItem.setModifyDate(new Date());
            existCartItem.setQuantity(existCartItem.getQuantity() + cartItem.getQuantity());
            cartItemMapper.updateById(existCartItem);
            return existCartItem;
        }
        return cartItem;
    }

    @Override
    public List<OmsCartItem> calcCartPromotion(List<OmsCartItem> cartItemList) {
        if (cartItemList == null && cartItemList.size() < 1) {
            throw new ApiMallPlusException("订单已提交");
        }


        return cartItemList;
    }

    /**
     * 查询所有商品的优惠相关信息
     */
    private List<PromotionProduct> getPromotionProductList(List<OmsCartItem> cartItemList) {
        List<Long> productIdList = new ArrayList<>();
        for (OmsCartItem cartItem : cartItemList) {
            productIdList.add(cartItem.getProductId());
        }
        return pmsProductMapper.getPromotionProductList(productIdList);
    }

    /**
     * 以spu为单位对购物车中商品进行分组
     */
    private Map<Long, List<OmsCartItem>> groupCartItemBySpu(List<OmsCartItem> cartItemList) {
        Map<Long, List<OmsCartItem>> productCartMap = new TreeMap<>();
        for (OmsCartItem cartItem : cartItemList) {
            List<OmsCartItem> productCartItemList = productCartMap.get(cartItem.getProductId());
            if (productCartItemList == null) {
                productCartItemList = new ArrayList<>();
                productCartItemList.add(cartItem);
                productCartMap.put(cartItem.getProductId(), productCartItemList);
            } else {
                productCartItemList.add(cartItem);
            }
        }
        return productCartMap;
    }

    /**
     * 获取满减促销消息
     */
    private String getFullReductionPromotionMessage(PmsProductFullReduction fullReduction) {
        StringBuilder sb = new StringBuilder();
        sb.append("满减优惠：");
        sb.append("满");
        sb.append(fullReduction.getFullPrice());
        sb.append("元，");
        sb.append("减");
        sb.append(fullReduction.getReducePrice());
        sb.append("元");
        return sb.toString();
    }

    /**
     * 对没满足优惠条件的商品进行处理
     */
    private void handleNoReduce(List<CartPromotionItem> cartPromotionItemList, List<OmsCartItem> itemList, PromotionProduct promotionProduct) {
        for (OmsCartItem item : itemList) {
            CartPromotionItem cartPromotionItem = new CartPromotionItem();
            BeanUtils.copyProperties(item, cartPromotionItem);
            cartPromotionItem.setPromotionMessage("无优惠");
            cartPromotionItem.setReduceAmount(new BigDecimal(0));
            PmsSkuStock skuStock = getOriginalPrice(promotionProduct, item.getProductSkuId());
            if (ValidatorUtils.notEmpty(skuStock)) {
                cartPromotionItem.setRealStock(skuStock.getStock() - skuStock.getLockStock());
            } else {
                PmsProduct pmsProduct = pmsProductMapper.selectById(item.getProductId());
                cartPromotionItem.setRealStock(pmsProduct.getStock());
            }
            cartPromotionItem.setIntegration(promotionProduct.getGiftPoint());
            cartPromotionItem.setGrowth(promotionProduct.getGiftGrowth());
            cartPromotionItemList.add(cartPromotionItem);
        }
    }

    private PmsProductFullReduction getProductFullReduction(BigDecimal totalAmount, List<PmsProductFullReduction> fullReductionList) {
        //按条件从高到低排序
        fullReductionList.sort(new Comparator<PmsProductFullReduction>() {
            @Override
            public int compare(PmsProductFullReduction o1, PmsProductFullReduction o2) {
                return o2.getFullPrice().subtract(o1.getFullPrice()).intValue();
            }
        });
        for (PmsProductFullReduction fullReduction : fullReductionList) {
            if (totalAmount.subtract(fullReduction.getFullPrice()).intValue() >= 0) {
                return fullReduction;
            }
        }
        return null;
    }

    /**
     * 获取打折优惠的促销信息
     */
    private String getLadderPromotionMessage(PmsProductLadder ladder) {
        StringBuilder sb = new StringBuilder();
        sb.append("打折优惠：");
        sb.append("满");
        sb.append(ladder.getCount());
        sb.append("件，");
        sb.append("打");
        sb.append(ladder.getDiscount().multiply(new BigDecimal(10)));
        sb.append("折");
        return sb.toString();
    }

    /**
     * 根据购买商品数量获取满足条件的打折优惠策略
     */
    private PmsProductLadder getProductLadder(int count, List<PmsProductLadder> productLadderList) {
        //按数量从大到小排序
        productLadderList.sort(new Comparator<PmsProductLadder>() {
            @Override
            public int compare(PmsProductLadder o1, PmsProductLadder o2) {
                return o2.getCount() - o1.getCount();
            }
        });
        for (PmsProductLadder productLadder : productLadderList) {
            if (count >= productLadder.getCount()) {
                return productLadder;
            }
        }
        return null;
    }

    /**
     * 获取购物车中指定商品的数量
     */
    private int getCartItemCount(List<OmsCartItem> itemList) {
        int count = 0;
        for (OmsCartItem item : itemList) {
            count += item.getQuantity();
        }
        return count;
    }

    /**
     * 获取购物车中指定商品的总价
     */
    private BigDecimal getCartItemAmount(List<OmsCartItem> itemList, List<PromotionProduct> promotionProductList) {
        BigDecimal amount = new BigDecimal(0);
        for (OmsCartItem item : itemList) {
            //计算出商品原价
            PromotionProduct promotionProduct = getPromotionProductById(item.getProductId(), promotionProductList);
            PmsSkuStock skuStock = getOriginalPrice(promotionProduct, item.getProductSkuId());
            amount = amount.add(skuStock.getPrice().multiply(new BigDecimal(item.getQuantity())));
        }
        return amount;
    }

    /**
     * 获取商品的原价
     */
    private PmsSkuStock getOriginalPrice(PromotionProduct promotionProduct, Long productSkuId) {
        for (PmsSkuStock skuStock : promotionProduct.getSkuStockList()) {
            if (productSkuId.equals(skuStock.getId())) {
                return skuStock;
            }
        }
        return null;
    }

    /**
     * 根据商品id获取商品的促销信息
     */
    private PromotionProduct getPromotionProductById(Long productId, List<PromotionProduct> promotionProductList) {
        for (PromotionProduct promotionProduct : promotionProductList) {
            if (productId.equals(promotionProduct.getId())) {
                return promotionProduct;
            }
        }
        return null;
    }

    @Override
    public Integer countCart(Long id) {
        return cartItemMapper.countCart(id);
    }

    @Override
    public Map<String, List<OmsCartItem>> listStoreCart(Long id) {
        List<OmsCartItem> list = cartItemMapper.selectList(new QueryWrapper<OmsCartItem>().eq("member_id", id));
        for (OmsCartItem item : list) {
            if (ValidatorUtils.notEmpty(item.getProductSkuId())) {
                item.setSkuStock(pmsSkuStockService.getById(item.getProductSkuId()));
            } else {
                item.setProduct(pmsProductMapper.selectById(item.getProductId()));
            }
        }
        Map<String, List<OmsCartItem>> map = list.stream().collect(Collectors.groupingBy(OmsCartItem::getStoreName));

        /*List<StoreCart> storeCartList = new ArrayList<>();
        for (Map.Entry<String, List<OmsCartItem>> entry : map.entrySet()) {
            StoreCart storeCart = new StoreCart();
            storeCart.setStoreName(entry.getKey());
            storeCart.setList(entry.getValue());
            storeCartList.add(storeCart);
        }*/

        return map;

    }

    @Override
    public PageInfo<CartItmes> getList(Long id, Integer pageIndex, Integer pageSize) {
        OmsCartItem example = new OmsCartItem();
        example.setMemberId(id);
        example.setDeleteStatus(0);
        IPage<OmsCartItem> page = new Page<>();

        page.setSize(pageSize);
        page.setCurrent(pageIndex.byteValue());
        QueryWrapper<OmsCartItem> queryWrapper = new QueryWrapper<>(example);
        queryWrapper.orderByDesc("create_date");
        IPage<OmsCartItem> omsCartItemIPage = cartItemMapper.selectPage(page, queryWrapper);

        List<OmsCartItem> records = omsCartItemIPage.getRecords();

        return packingCar(records, pageIndex, pageSize);
    }

    @Override
    public void cartRemoveFavorites(Long memberId, String id) {
        String[] split = id.split(",");

        List<Long> ids = new ArrayList<>();

        for (String s : split) {
            ids.add(Long.valueOf(s));
        }


        List<OmsCartItem> list = list(memberId, ids);

        if (list.size() > 0) {
            for (OmsCartItem omsCartItem : list) {
                Long productId = omsCartItem.getProductId();
                //此时商品是否在以前就收藏过
                PmsFavorite myFavorite = findMyFavorite("obj_id", productId, "member_id", memberId);

                //等于空说明没有收藏
                if (myFavorite == null) {
                    PmsProduct pmsProduct = pmsProductMapper.selectById(productId);
                    PmsFavorite favorite = new PmsFavorite();

                    favorite.setType(1);
                    favorite.setObjId(productId);
                    favorite.setMemberId(memberId);
                    favorite.setAddTime(new Date());
                    favorite.setName(pmsProduct.getName());
                    favorite.setMeno1(pmsProduct.getPic());
                    favorite.setMeno3(String.valueOf(pmsProduct.getSale()));
                    favorite.setMeno2(String.valueOf(pmsProduct.getPrice()));

                    pmsFavoriteMapper.insert(favorite);
                    cartItemMapper.deleteById(omsCartItem.getId());
                } else {
                    cartItemMapper.deleteById(omsCartItem.getId());
                }

            }
        }


    }

    @Override
    public Integer promoteAmount(Long id) {
        List<OmsCartItem> member_id = findAllCartSum("member_id", id);

        return member_id.size();
    }

    private List<OmsCartItem> findAllCartSum(String cloumn, Object value) {
        QueryWrapper<OmsCartItem> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(cloumn, value);

        return cartItemMapper.selectList(queryWrapper);
    }

    private PmsFavorite findMyFavorite(String column, Object value, String column1, Object value1) {
        QueryWrapper<PmsFavorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, value).eq(column1, value1);
        return pmsFavoriteMapper.selectOne(queryWrapper);
    }


}
